package com.noble.meether.ui.apply;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.noble.meether.R;
import com.noble.glamour.bean.center.user.detail.UserDetail;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.LoadingDialog;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.log.PToast;
import com.noble.library.utils.UIUtil;
import com.noble.meether.logic.center.EditKey;

import java.util.HashMap;

/**
 * 提现账户
 */
public class ApplyAccountAct extends BaseAppCompatActivity implements RequestComplete, View.OnClickListener {
    private EditText et_account, et_name;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_money_account);
        initView();
    }

    private void initView() {
        setBackView("添加账户");
        et_account = findViewById(R.id.et_account);
        et_name = findViewById(R.id.et_name);
        findViewById(R.id.btn_add).setOnClickListener(this);

        UserDetail userDetail = ModuleMgr.getCenterMgr().getMyInfo();
        if (!TextUtils.isEmpty(userDetail.getAlipay())) {
            et_account.setText(userDetail.getAlipay());
            UIUtil.endCursor(et_account);
        }
        if (!TextUtils.isEmpty(userDetail.getRealName())) {
            et_name.setText(userDetail.getRealName());
        }
    }

    private void addAccount() {
        String account = et_account.getText().toString();
        if (TextUtils.isEmpty(account)) {
            PToast.showShort("请输入支付宝账户");
            return;
        }
        String name = et_name.getText().toString();
        if (TextUtils.isEmpty(name)) {
            PToast.showShort("请输入真实姓名");
            return;
        }
        HashMap<String, Object> param = new HashMap<>();
        param.put(EditKey.s_key_alipay, account);
        param.put(EditKey.s_key_realName, name);
        LoadingDialog.show(this, "正在修改，请稍候...");
        ModuleMgr.getCenterMgr().updateMyInfo(param, this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (!response.isOk()) {
            return;
        }
        ModuleMgr.getCenterMgr().reqMyInfo("ApplyAccountAct");
        finish();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_add:
                addAccount();
                break;
        }
    }
}